//
//  YYSUserController.m
//  FanLaiWu
//
//  Created by gonglei on 17/3/17.
//  Copyright © 2017年 yys. All rights reserved.
//

#import "YYSUserController.h"
@interface YYSUserController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIView *iconView;
@property (weak, nonatomic) IBOutlet UITextField *userNameText;
@property (weak, nonatomic) IBOutlet UIImageView *iconImage;
@property (nonatomic, strong) UIImagePickerController *pickerVC;
@end

@implementation YYSUserController
- (IBAction)clickSaveBtn:(id)sender {
    [[SaveManager shareSaveManager] updateUserName:self.userNameText.text];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    label.text = @"个人资料";
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = label;
    self.userNameText.text = [[SaveManager shareSaveManager] getUserName];
    
    self.iconImage.layer.masksToBounds = YES;
    self.iconImage.layer.cornerRadius = self.iconImage.yy_width *0.5;
    
    [self.iconView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickIcon:)]];
}
- (void)clickIcon:(UITapGestureRecognizer *)tap
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *imageAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self selectImageFromAlbum];
    }];
    
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self selectImageFromCamera];
    }];
    
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alertController addAction:imageAction];
    [alertController addAction:cameraAction];
    [alertController addAction:cancleAction];
    
    [self presentViewController:alertController animated:YES completion:^{
        
    }];
    
}
#pragma mark -- UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo {
    
//    SelectImageViewController *selectImageVC = [[SelectImageViewController alloc] init];
//
//    //    image = [image ChangeImageWithSize];
//    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//    imageView.contentMode = UIViewContentModeScaleToFill;
//    selectImageVC.imageView.image = image;
//    selectImageVC.fromUpload = @"changeHeadImage";
    self.iconImage.image = image;
    self.center.iconImage = image;
    [picker dismissViewControllerAnimated:NO completion:nil];
//    [self.navigationController pushViewController:selectImageVC animated:YES];
    
}

- (void)selectImageFromCamera
{
    self.pickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
    //录制视频时长，默认10s
    self.pickerVC.videoMaximumDuration = 15;
    
    //相机类型（拍照、录像...）字符串需要做相应的类型转换
    
    self.pickerVC.mediaTypes = @[(NSString *)kUTTypeMovie,(NSString *)kUTTypeImage];
    
    //视频上传质量
    //UIImagePickerControllerQualityTypeHigh高清
    //UIImagePickerControllerQualityTypeMedium中等质量
    //UIImagePickerControllerQualityTypeLow低质量
    //UIImagePickerControllerQualityType640x480
    self.pickerVC.videoQuality = UIImagePickerControllerQualityTypeHigh;
    
    //设置摄像头模式（拍照，录制视频）为录像模式
    self.pickerVC.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    [self presentViewController:self.pickerVC animated:YES completion:nil];
}

- (void)selectImageFromAlbum
{
    //NSLog(@"相册");
    self.pickerVC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    //相机类型（拍照、录像...）字符串需要做相应的类型转换
    
    self.pickerVC.mediaTypes = @[(NSString *)kUTTypeImage];
    [self presentViewController:self.pickerVC animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (UIImagePickerController *)pickerVC {
    if (!_pickerVC) {
        _pickerVC = [[UIImagePickerController alloc] init];
        _pickerVC.delegate = self;
        _pickerVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    }
    return _pickerVC;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
